<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Selling extends My_Controller {

    function __construct() {
        parent::__construct();
    }

    public function index() {
        $this->navigation->loadSalesAppraisalView();
    }

    public function gogecko() {
        $this->load->model('au_suburb_geo_model', 'suburb');
        $this->load->model('franchise_model', 'franchise');
        $posts = $this->input->post();

        $data['post'] = $posts;
        //var_dump($posts);die;
        $data['public_key'] = $this->config->item('public_key');
        $data['error'] = null;
        $this->load->helper('email');
        $this->load->helper('sendmail');
        if ($posts) {
            //var_dump($posts);die;
            $suburb_id = !empty($posts['address_suburb_id']) ? $posts['address_suburb_id'] : "";
            //$suburd_Geo = $this->suburb->getSuburbById($suburb_id);
            if (!empty($suburb_id)) {
                $suburd_Geo = $this->suburb->getSuburbById($suburb_id);
                $suburbGeo = $this->suburb->getSuburbGeoById($suburb_id);
                //            if (!empty($suburbGeo)){}
                //                $data['office'] = $this->franchise->getOfficeNearest($suburbGeo->Lat, $suburbGeo->Long);
                if (!empty($suburd_Geo)) {
                    $emailTo = $suburd_Geo->email;
                    //var_dump($suburd_Geo);die;
                } else {
                    if (!empty($suburbGeo)) {
                        $data['office'] = $this->franchise->getOfficeNearest($suburbGeo->Lat, $suburbGeo->Long);
                        $emailTo = $data['office']->email;
                    }
                }
            }
            //var_dump($suburd_Geo);die;
            //$emailTo = !empty($suburd_Geo->email) ? $suburd_Geo->email : null;
            $privateKey = $this->config->item('private_key');
            $validationErrors = array();
            if ($posts['suburb'] == '') {
                $validationErrors['form-suburbs'] = "Suburb cannot be blank";
            }
            if ($posts['address'] == '') {
                $validationErrors['address-street'] = "Address cannot be blank";
            }
            if ($posts['name'] == '') {
                $validationErrors['name'] = "Your Name cannot be blank";
            }
            if ($posts['phone'] == '') {
                $validationErrors['phone'] = "Contact Number cannot be blank";
            }
            if ($posts['email'] == '') {
                $validationErrors['emailAddress'] = "Email cannot be blank";
            } elseif (!valid_email($posts['email'])) {
                $validationErrors['emailAddress'] = "Email is not valid";
            }

            if ((!isset($posts['recaptcha_challenge_field']) || !isset($posts['recaptcha_challenge_field'])) && !isset($posts['captcha_code'])) {
                $validationErrors['recaptcha_response_field'] = 'Please enter captcha';
            }

            if (isset($posts['recaptcha_challenge_field']) && isset($posts['recaptcha_challenge_field'])) {
                $resp = recaptcha_check_answer($privateKey, $_SERVER["REMOTE_ADDR"], $posts["recaptcha_challenge_field"], $posts["recaptcha_response_field"]);
                if (!$resp->is_valid) {
                    $validationErrors['recaptcha_response_field'] = $resp->error;
                }
            }

            if (isset($posts['captcha_code'])) {
                $currentCaptcha = $this->session->userdata('captcha_mobile');
                if (strtolower($currentCaptcha) != strtolower($posts['captcha_code'])) {
                    $validationErrors['captcha_code'] = 'Incorrect Captcha';
                }
            }

            if (count($validationErrors) != 0) {
                $data['usermessage'] = array('error', 'Please complete all required fields');
                $data['fielderrors'] = $validationErrors;
            } else {
                $content = '<b>Suburb</b> :' . $posts['suburb'] . '<br>';
                $content .= '<b>Your Name </b> :' . $posts['name'] . '<br>';
                $content .= '<b>Contact Number</b> :' . $posts['phone'] . '<br>';
                $content .= '<b>Mobile</b>  :' . $posts['mobile'] . '<br>';
                $content .= '<b>Email</b>  :' . $posts['email'] . '<br>';
                $content .= '<b>Address</b>  :' . $posts['address'] . ', ' . $posts['address_suburb'] . '<br>';
                $content .= '<b>Type</b>  :' . $posts['propertyType'] . '<br>';
                $content .= '<b>Bedrooms</b>   :' . $posts['bedrooms'] . '<br>';
                $content .= '<b>Bathrooms</b>   :' . $posts['bathrooms'] . '<br>';
                $content .= '<b>How did you hear about us?</b>  :' . $posts['your_hear'] . ' ' . $posts['hear_other'] . '<br>';
                if (@sendmail($emailTo, 'Free Market Appraisal - Sale', $content, $posts['email'], $posts['name'])) {
                    $data['usermessage'] = array('success', 'Thank you - Your email request is being sent and a Sales Agent will be in contact with you directly', '');
                    $data['post'] = '';
                } else {
                    $data['usermessage'] = array('error', 'Email sent Error');
                }
            }
        }
        $this->navigation->loadSellingWithGoGeckoView($data);
    }

    public function sold() {

        //data for quick search
        $data['propertyType'] = $this->site->getArray('propertyType', 'Any');
        $data['BedroomCount'] = $this->site->getArray('featuteCount', 'Any');
        $data['BathroomCount'] = $this->site->getArray('featuteCount', 'Any');
        $data['Minprice'] = $this->site->getArray('priceRange', 'Any');
        $data['Maxprice'] = $this->site->getArray('priceRange', 'Any');
        $data['subTitle'] = 'Properties Sold';
        $data['type'] = 'sold';


        if ($this->input->post()) {
            $data['seachval_type'] = $this->input->post('type');
            $data['seachval_surroundingSuburbs'] = $this->input->post('surroundingSuburbs');
            $data['seachval_suburbs'] = $this->input->post('suburbs');
            $data['search_propertytype'] = $this->input->post('search_propertytype');
            $data['search_minprice'] = $this->input->post('search_minprice');
            $data['search_maxprice'] = $this->input->post('search_maxprice');
            $data['search_bathroom'] = $this->input->post('search_bathroom');
            $data['search_bedroom'] = $this->input->post('search_bedroom');
            $data['suburbs_id'] = $this->input->post('suburbs_id');
            $data['suburbs'] = $this->input->post('suburbs');
            $data['surrounding'] = $this->input->post('surrounding');
            $data['sortBy'] = $this->input->post('sortBy');
            $data['orderBy'] = $this->input->post('orderBy');
        }

        // $this->navigation->loadListingsView($data);
        $this->navigation->loadPropertiesSoldView($data);
    }

    public function around_suburbs() {
        $this->load->model('au_suburb_geo_model', 'suburb');
        $posts = $this->input->post();
        $data['post'] = $posts;
        //var_dump($posts);die;
        if ($posts) {
            //var_dump($posts);die;
            $suburb_id = !empty($posts['suburbs_id']) ? $posts['suburbs_id'] : "";
            $au_suburb_geo = $this->suburb->getAuSuburbById($suburb_id);
            //var_dump($au_suburb_geo);die;
            $state = $au_suburb_geo->State1;
            $locality = $au_suburb_geo->Locality;
            $lat = $au_suburb_geo->Lat;
            $long = $au_suburb_geo->Long;
            $pcode = $au_suburb_geo->Pcode;
            //var_dump($state);die;
            if ($lat != null && $long != null) {
                $suburd_around = $this->suburb->getSuburbAround($lat, $long);
            }
            //var_dump($suburd_around);die;
            $data['Locality'] = $locality . ' ' . $state . ' ' . $pcode;
            $data['suburb_around'] = $suburd_around;
            //$this->load->view('selling/around_suburbs',$data);
        }
        $this->navigation->loadPropertiesAroundView($data);
    }

    public function appraisal() {
        $this->load->model('au_suburb_geo_model', 'suburb');
        $this->load->model('franchise_model', 'franchise');
        $posts = $this->input->post();

        $data['post'] = $posts;
        //var_dump($posts);die;
        $data['public_key'] = $this->config->item('public_key');
        $data['error'] = null;
        $this->load->helper('email');
        $this->load->helper('sendmail');
        if ($posts) {
            //var_dump($posts);die;
            $suburb_id = !empty($posts['address_suburb_id']) ? $posts['address_suburb_id'] : "";
            //$suburd_Geo = $this->suburb->getSuburbById($suburb_id);
            if (!empty($suburb_id)) {
                $suburd_Geo = $this->suburb->getSuburbById($suburb_id);
                $suburbGeo = $this->suburb->getSuburbGeoById($suburb_id);
                //            if (!empty($suburbGeo)){}
                //                $data['office'] = $this->franchise->getOfficeNearest($suburbGeo->Lat, $suburbGeo->Long);
                if (!empty($suburd_Geo)) {
                    $emailTo = $suburd_Geo->email;
                    //var_dump($suburd_Geo);die;
                } else {
                    if (!empty($suburbGeo)) {
                        $data['office'] = $this->franchise->getOfficeNearest($suburbGeo->Lat, $suburbGeo->Long);
                        $emailTo = $data['office']->email;
                    }
                }
            }
            //var_dump($suburd_Geo);die;
            //$emailTo = !empty($suburd_Geo->email) ? $suburd_Geo->email : null;
            $privateKey = $this->config->item('private_key');
            $validationErrors = array();
            if ($posts['suburb'] == '') {
                $validationErrors['form-suburbs'] = "Suburb cannot be blank";
            }
            if ($posts['address'] == '') {
                $validationErrors['address-street'] = "Address cannot be blank";
            }
            if ($posts['name'] == '') {
                $validationErrors['name'] = "Your Name cannot be blank";
            }
            if ($posts['phone'] == '') {
                $validationErrors['phone'] = "Contact Number cannot be blank";
            }
            if ($posts['email'] == '') {
                $validationErrors['emailAddress'] = "Email cannot be blank";
            } elseif (!valid_email($posts['email'])) {
                $validationErrors['emailAddress'] = "Email is not valid";
            }

            $resp = recaptcha_check_answer($privateKey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
            if (!$resp->is_valid) {
                $validationErrors['recaptcha_response_field'] = $resp->error;
            }
            if (count($validationErrors) != 0) {
                $data['usermessage'] = array('error', 'Please complete all required fields');
                $data['fielderrors'] = $validationErrors;
            } else {
                $content = '<b>Suburb</b> :' . $posts['suburb'] . '<br>';
                $content .= '<b>Your Name </b> :' . $posts['name'] . '<br>';
                $content .= '<b>Contact Number</b> :' . $posts['phone'] . '<br>';
                $content .= '<b>Mobile</b>  :' . $posts['mobile'] . '<br>';
                $content .= '<b>Email</b>  :' . $posts['email'] . '<br>';
                $content .= '<b>Address</b>  :' . $posts['address'] . ', ' . $posts['address_suburb'] . '<br>';
                $content .= '<b>Type</b>  :' . $posts['propertyType'] . '<br>';
                $content .= '<b>Bedrooms</b>   :' . $posts['bedrooms'] . '<br>';
                $content .= '<b>Bathrooms</b>   :' . $posts['bathrooms'] . '<br>';
                $content .= '<b>How did you hear about us?</b>  :' . $posts['your_hear'] . ' ' . $posts['hear_other'] . '<br>';
                if (sendmail($emailTo, 'Free Market Appraisal - Sale', $content, $posts['email'], $posts['name'])) {
                    $data['usermessage'] = array('success', 'Thank you - Your email request is being sent and a Sales Agent will be in contact with you directly', '');
                    $data['post'] = '';
                } else {
                    $data['usermessage'] = array('error', 'Email sent Error');
                }
            }
        }
        $this->navigation->loadSalesAppraisalView($data);
    }

    public function presenting() {
        $this->navigation->loadPresentingYourPropertyView();
    }

}

/* End of file selling.php */
/* Location: ./application/controllers/selling.php */